cap log close
set more off
clear
clear matrix
version 17


/*--------------------------------------------------------------------------------------*/
* Do-file reproducing all results of the paper:

******************************************************************************************
*"The Impact of Working Memory Training on Children's Cognitive and Noncognitive Skills"**
********************************** by ****************************************************
**** Daniel Schunk, Eva M. Berger, Henning Hermes, Kirsten Winkel & Ernst Fehr ***********
******************************* April 2024 ***********************************************
******************************************************************************************

/*--------------------------------------------------------------------------------------*/




* Set PATH to directory where zip-file was unpacked:
global PATH "/Users/Example/Folder/"

cd "${PATH}"

* Install required packages
ssc install estout, replace
ssc install rowsort, replace
ssc install grstyle, replace
ssc install palettes, replace
ssc install colrspace, replace
ssc install coefplot, replace
net install grc1leg, from(http://www.stata.com/users/vwiggins/) replace
* Also install brl command from https://economics.byu.edu/faculty-and-staff/frandsen/software
* to personal Stata folder.




* Load data set
clear all
use "${PATH}Replication Data Set WMT.dta", clear




*******************************************************************************************
********************** PART 1: Reproduce Figures from Main Paper **************************
*******************************************************************************************

* Set graphical style
grstyle init
grstyle set compact
grstyle color background white




****************** Run necessary regressions for Figures 1, 2, and 3 ******************
est clear
eststo clear

foreach o in Zw Obw Odw M13 M4 L R GfA Bpscore {
	
	foreach w in w2 w3 w4 { 
		
		qui reg `w'`o'std WMTp MCIIp C2p WMTMCII C2MCII w1`o'std school2-school12 male agechild w1agetest `w'agetestday, vce(cluster classID)
		est store `w'`o'1

	}
}


******* Figure 1: Direct Effect of Training on Working Memory Capacity

** Create the Figure
coefplot 	(w2Zw1, keep(WMTp) label(Shortly after training (W2)) ) (w3Zw1, keep(WMTp) ///
		label(Six months after training (W3)) mlabels(WMTp = 3 ***)) (w4Zw1, keep(WMTp) ///
		label(12-13 months after training (W4)) mlabels(WMTp = 3 **) legend(pos(6))), ///
		bylabel(Verbal simple span) || (w2Obw1, keep(WMTp)) ///
		(w3Obw1, keep(WMTp)) (w4Obw1, keep(WMTp) legend(off)), /// 
		bylabel(Verbal complex span) || (w2Odw1, keep(WMTp) mlabels(WMTp = 3 ***) legend(off)) (w3Odw1, keep(WMTp) ///
		mlabels(WMTp = 3 ***) legend(off)) (w4Odw1, keep(WMTp) mlabels(WMTp = 3 ***) legend(off)), ///
		bylabel(Visuo-spatial complex sp.) ||, ///
		vertical yline(0) ylabel(-0.4(0.2)0.7) ytick(-0.4(0.1)0.7, grid) byopts(rows(1) title("Figure 1: Direct Effect of Training on Working Memory Capacity", size(normal))) ciopts(recast(rcap)) ///
		ytitle(Treatment effect in SD) rename(WMTp=" ") nolabels 
		
	
	
******* Figure 2: Spillover Effects of Arithmetic, Geometry, Reading, and Raven's IQ

** Create the Figure
coefplot (w2M131, keep(WMTp) label(Shortly after training (W2)))   (w3M131, keep(WMTp) label(Six months after training (W3)))   (w4M131, keep(WMTp) label(12-13 months after training (W4))), bylabel(Arithmetic)  ///
	||   (w2M41, keep(WMTp))   (w3M41, keep(WMTp) mlabels(WMTp = 3 **))   (w4M41, keep(WMTp) mlabels(WMTp = 3 ***)), bylabel(Geometry) ///
	||   (w2L1, keep(WMTp))   (w3L1, keep(WMTp))   (w4L1, keep(WMTp) mlabels(WMTp = 3 **)),  bylabel(Reading) ///
	||   (w2R1, keep(WMTp))   (w3R1, keep(WMTp) mlabels(WMTp = 3 ***))   (w4R1, keep(WMTp) mlabels(WMTp = 3 ***)), bylabel(Raven's IQ) || ///
	, vertical yline(0) ylabel(-0.4(0.2)0.7) ytick(-0.4(0.1)0.7, grid) byopts(rows(1) title("Figure 2: Spillover Effects of Arithmetic, Geometry, Reading, and Raven's IQ", size(middle))) ciopts(recast(rcap)) ytitle(Treatment effect in SD) rename(WMTp=" ") nolabels
	
	
	
	
******* Figure 3: Spillover Effects in the Go/No-Go Task and the bp Task

** Create the Figure
coefplot (w2GfA1, keep(WMTp) label(Shortly after training (W2)))   (w3GfA1, keep(WMTp) label(Six months after training (W3)))   (w4GfA1, keep(WMTp) label(12-13 months after training (W4)) mlabels(WMTp = 3 ***)), bylabel(Go/No-Go task) ///
	||   (w2Bpscore1, keep(WMTp))   (w3Bpscore1, keep(WMTp))   (w4Bpscore1, keep(WMTp)), bylabel(bp task) || ///
	, vertical yline(0) ylabel(-0.4(0.2)0.7) ytick(-0.4(0.1)0.7, grid) byopts(rows(1) title("Figure 3: Spillover Effects in the Go/No-Go Task and the bp Task", size(middle))) ciopts(recast(rcap)) ytitle(Treatment effect in SD) rename(WMTp=" ") nolabels


	
	
******* Figure 4: The Relative Importance of Working Memory Capacity for the Treatment Effects on Spillover Outcomes

est clear
eststo clear

** Set graphical style
grstyle init
grstyle set color economist, n(3) reverse
grstyle color background white

** Run necessary regressions
preserve

* TE on Mediators

qui reg w4Odwstd WMTp MCIIp C2p WMTMCII C2MCII school2-school12 male agechild w1Odwstd w4agetestday w1agetestday, vce(cluster classID)
local alpha1 = _b[WMTp]

qui reg w4Zwstd WMTp MCIIp C2p WMTMCII C2MCII school2-school12 male agechild w1Zwstd w4agetestday w1agetestday, vce(cluster classID)
local alpha2 = _b[WMTp]


* Mediators on Outcomes

qui foreach v in M4 L R GfA {

	reg w4`v'std w4Odwstd w4Zwstd school2-school12 male agechild w1`v'std w4agetestday 	w1agetestday if WMTp == 0, vce(cluster classID)
	local beta1`v' = _b[w4Odwstd]
	local beta2`v' = _b[w4Zwstd]

}


* TE on Outcomes

qui foreach v in M4 L R GfA {

	reg w4`v'std WMTp MCIIp C2p WMTMCII C2MCII school2-school12 male agechild w1`v'std 	w4agetestday w1agetestday, vce(cluster classID)
	local TE`v' = _b[WMTp]

}

* Generate shares

qui foreach v in M4 L R GfA {

	gen shareall_w4`v' = (`alpha1' * `beta1`v'' + `alpha2' * `beta2`v'')/`TE`v''
	gen rest_w4`v' = 1 - shareall_w4`v'
	
}


* Generate graphs

*W4
graph 	hbar shareall_w4M4 rest_w4M4, stack bargap(1) blabel(bar,position(center) format(%3.2f) color(white))  ///
		legend(bmargin(zero) rows(4) symxsize(*0.5) size(vsmall) ///
		label(1 "Working Memory Capacity") label(2 "Unobserved Factors"))  ///
		yscale(range(0(0.2)1) noextend) ylabel(, labsize(vsmall) ) aspectratio(0.15) ytitle(" " "Geometry (W4)", size(medium)) ///
		name(gw4M4, replace) nodraw
		*title("Geometry (W4)", size(small) position(12))

graph 	hbar shareall_w4L rest_w4L, stack bargap(1) blabel(bar,position(center) format(%3.2f) color(white))  ///
		legend(bmargin(zero) rows(4) symxsize(*0.5) size(vsmall) ///
		label(1 "Working Memory Capacity") label(2 "Unobserved Factors")) ///
		yscale(range(0(0.2)1) noextend) ylabel(, labsize(vsmall)) aspectratio(0.15) ytitle(" " "Reading (W4)", size(medium)) ///
		name(gw4L, replace) nodraw
		*title("Reading (W4)"

graph 	hbar shareall_w4R rest_w4R, stack bargap(1) blabel(bar,position(center) format(%3.2f) color(white))  ///
		legend(bmargin(zero) rows(4) symxsize(*0.5) size(vsmall) ///
		label(1 "Working Memory Capacity") label(2 "Unobserved Factors")) ///
		yscale(range(0(0.2)1) noextend) ylabel(, labsize(vsmall)) aspectratio(0.15) ytitle(" " "Raven's IQ (W4)", size(medium)) ///
		name(gw4R, replace) nodraw
		*title("Raven's IQ (W4)	, size(small) position(9))"

graph 	hbar shareall_w4GfA rest_w4GfA, stack bargap(1) blabel(bar,position(center) format(%3.2f) color(white))  ///
		legend(bmargin(zero) rows(4) symxsize(*0.5) size(vsmall) ///
		label(1 "Working Memory Capacity") label(2 "Unobserved Factors")) ///
		yscale(range(0(0.2)1) noextend ) ylabel(, labsize(vsmall)) aspectratio(0.15) ytitle(" " "Go/No-Go task (W4)", size(medium)) ///
		name(gw4GfA, replace) nodraw
		*title("Go/No-Go task (W4)	, size(small) position(9))"
		

* Combine graphs						
grc1leg gw4M4 gw4L gw4R gw4GfA, cols(1) span  name(gALL, replace)
graph display gALL, ysize(2) xsize(1.4)				

		
restore 




*******************************************************************************************
********************** PART 2: Reproduce Tables from Main Paper **************************
*******************************************************************************************




******* Table 1: Summary Statistics

qui estpost tabstat WMTp male agechild w1agetestday w2agetestday w3agetestday w4agetestday mig pbsprache12 income1 income2 income3 income4 income5 Muni Mausb Mkausb gymE igsE realE, stats(mean sd n min max p10 p90) columns(statistics)

esttab ., cells("mean(fmt(3)) sd(fmt(3))  min(fmt(3)) max(fmt(3)) count(fmt(%8.0f))") nonumbers title(Table 1: Summary Statistics) collabels("Mean" "Std. Dev." "Min." "Max." "N") label noobs 




******* Table 2: Treatment Effect on Secondary School Choice at Age 10

est clear
eststo clear

** Run necessary regressions
qui reg gymE WMTp MCIIp C2p WMTMCII C2MCII school2-school12 male agechild, vce(cluster classID)
est store OLS

qui probit gymE WMTp MCIIp C2p WMTMCII C2MCII school2-school12 male agechild, vce(cluster classID)
eststo gymE2: qui margins, dydx(*) predict() post
est store PROBIT	

qui reg decisionE WMTp MCIIp C2p WMTMCII C2MCII school2-school12 male agechild, vce(cluster classID)
est store OLScat

qui oprobit decisionE WMTp MCIIp C2p WMTMCII C2MCII school2-school12 male agechild, vce(cluster classID)
eststo OPROBIT: qui margins, dydx(WMTp) predict() post


*Generate weighting for IPW regression
foreach x in w1Lstd w1M13std w1M4std w1WMwstd w1Rstd {
	
	clonevar `x'_imp = `x'
	replace `x'_imp = 0 if `x'_imp == .
	
}

clonevar mig_imp = mig
qui sum mig, det
replace mig_imp = 0 if mig_imp == .

gen w1acperf_imp=w1Lstd_imp+w1M13std_imp+w1M4std_imp
qui sum w1acperf_imp, d
gen w1acperf_impHigh=w1acperf_imp>=r(p50) if w1acperf_imp<.

gen w1cog_imp=w1WMwstd_imp+w1Rstd_imp
qui sum w1cog_imp, d
gen w1cog_impHigh=w1cog_imp>=r(p50) if w1cog_imp<.

gen cat=.
local n=1

forval a=0/1 {
	
	forval b=0/1 { 
		
		forval c=0/1 { 
			
			replace cat=`n' if mig_imp==`a' & w1acperf_impHigh==`b' & w1cog_impHigh==`c'
			local n=`n'+1
		}
	}
}

gen pweight_imp=.

forval x=1/8 {
	
	qui sum gymEnonmiss if cat==`x'
	replace pweight_imp=1/r(mean) if cat==`x' & gymEnonmiss==1
	
}

*Run IPW regression
qui reg gymE WMTp MCIIp C2p WMTMCII C2MCII school2-school12 male agechild [pweight=pweight_imp], vce(cluster classID) 
est store pweightimp

**Create Table
esttab OLS PROBIT OLScat OPROBIT pweightimp  ///
	, rename(WMTp "Academic track School" 3._predict "Academic track School" 2._predict "Mixed track school" 1._predict "Non-academic track school") ///
	order("Academic track School" "Mixed track school" "Non-academic track school") ///
	cells(b(star fmt(%9.3f)) se(par fmt(3))) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table 2: Treatment Effect on Secondary School Choice at Age 10) ///
	eqlabels(" " " ") mtitles("OLS" "Probit" "OLS cat var" "Ordered Probit" "Inverse Prob Weighting") collabels(none) label drop( _cons school* MCIIp C2p WMTMCII C2MCII male agechild)

	
*Run regression using BRL
brl gymE WMTp MCIIp C2p WMTMCII C2MCII school2-school12 male agechild, cluster(classID)



*******************************************************************************************
********************** PART 3: Reproduce Figures from Appendix **************************
*******************************************************************************************

grstyle init
grstyle color background white
grstyle set compact




******* Figure S12: Distribution of Nonstandardized W1 Test Scores

est clear
eststo clear

qui foreach x in M4 L R GfA {
	
	count if w1`x'<.
	local Nw1`x'=r(N)
	histogram w1`x', discrete fraction lcolor(gs4) fcolor(gs0) name(graph`x', replace) note(N=`Nw1`x'') nodraw
		
}
		
qui foreach x in Zw Obw Odw M13 Bpscore {
	
	count if w1`x'<.
	local Nw1`x'=r(N)
	histogram w1`x', discrete fraction lcolor(gs0) fcolor(gs0) name(graph`x', replace) note(N=`Nw1`x'') nodraw
		
}
				
graph combine graphZw graphObw graphOdw graphM13 graphM4 graphL graphR graphGfA graphBpscore, rows(4) title("Figure S12: Distribution of Nonstandardized W1 Test Scores", size(medium) position(6)) 




******* Figure S13: Distribution of Nonstandardized W2 Test Scores

est clear
eststo clear

qui foreach x in M4 L R GfA {
	
	count if w2`x'<.
	local Nw2`x'=r(N)
	histogram w2`x', discrete fraction lcolor(gs4) fcolor(gs0) name(graph`x', replace) note(N=`Nw2`x'') nodraw
		
}
		
qui foreach x in Zw Obw Odw M13 Bpscore {
	
	count if w2`x'<.
	local Nw2`x'=r(N)
	histogram w2`x', discrete fraction lcolor(gs0) fcolor(gs0) name(graph`x', replace) note(N=`Nw2`x'') nodraw
		
}
	
graph combine graphZw graphObw graphOdw graphM13 graphM4 graphL graphR graphGfA graphBpscore, rows(4) title("Figure S13: Distribution of Nonstandardized W2 Test Scores", size(medium) position(6))




******* Figure S14: Distribution of Nonstandardized W3 Test Scores

est clear
eststo clear

qui foreach x in M4 L R GfA {
	
		count if w3`x'<.
		local Nw3`x'=r(N)
		histogram w3`x', discrete fraction lcolor(gs4) fcolor(gs0) name(graph`x', replace) note(N=`Nw3`x'') nodraw
		
}
		
qui foreach x in Zw Obw Odw M13 Bpscore {
	
		count if w3`x'<.
		local Nw3`x'=r(N)
		histogram w3`x', discrete fraction lcolor(gs0) fcolor(gs0) name(graph`x', replace) note(N=`Nw3`x'') nodraw
		
}
	
graph combine graphZw graphObw graphOdw graphM13 graphM4 graphL graphR graphGfA graphBpscore, rows(4) title("Figure S14: Distribution of Nonstandardized W3 Test Scores", size(medium) position(6))




******* Figure S15: Distribution of Nonstandardized W4 Test Scores

est clear
eststo clear

qui foreach x in M4 L R GfA {
	
		count if w4`x'<.
		local Nw4`x'=r(N)
		histogram w4`x', discrete fraction lcolor(gs4) fcolor(gs0) name(graph`x', replace) note(N=`Nw4`x'') nodraw
		
}
qui foreach x in Zw Obw Odw M13 Bpscore {
	
		count if w4`x'<.
		local Nw4`x'=r(N)
		histogram w4`x', discrete fraction lcolor(gs0) fcolor(gs0) name(graph`x', replace) note(N=`Nw4`x'') nodraw
		
}
	
graph combine graphZw graphObw graphOdw graphM13 graphM4 graphL graphR graphGfA graphBpscore, rows(4) title("Figure S15: Distribution of Nonstandardized W4 Test Scores", size(medium) position(6))




******* Figure S16: Parental Investment in W3 by Treatment Status

count if W3_investment<.
local N=r(N)
histogram W3_investment, discrete fraction by(WMTp, title("Figure S16: Parental Investment in W3 by Treatment Status", size(medium) position(6)) note("")) lcolor(gs4) fcolor(gs0) xlabel(1(2)15) xtitle("")




******* Figure S17: Children's Enjoyment in W4 Tasks

count if w4_Q1<. & w4_Q1>0
local NQ1=r(N)
histogram w4_Q1 if w4_Q1>0, discrete fraction fcolor(gs0) xtitle("") note(N=`NQ1') title("Figure S17: Children's Enjoyment in W4 Tasks", size(medium) position(6))




******* Figure S18: Children's Effort in W4 Tasks

count if w4_Q2<. & w4_Q2>0
local NQ2=r(N)
histogram w4_Q2 if w4_Q2>0, discrete fraction fcolor(gs0) xtitle("") note(N=`NQ2') title("Figure S18: Children's Effort in W4 Tasks", size(medium) position(6))




*******************************************************************************************
********************** PART 4: Reproduce Tables from Appendix **************************
*******************************************************************************************




******* Table S1: Sample Balance: Regressing Socio-demographic Characteristics on the Treatment Indicator

est clear
eststo clear

** Run necessary regressions
qui foreach o in  male agechild mig pbsprache12 income12 income45 Mkausb Muni { 
	
	reg `o' WMTp MCIIp C2p WMTMCII C2MCII school2-school12, vce(cluster classID)
	est store `o'
	
}

** Generate table with regression results
esttab male agechild mig pbsprache12 income12 income45 Mkausb Muni, ///
	nogaps b(3) se(3) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S1: Sample Balance: Regressing Socio-demographic Characteristics on the Treatment Indicator) ///
	mtitle("Male" "Age in months" "Migration Backgr." "Language Probl." "Income < 1500" "Income > 2500" "Mother no degree" "Mother Univ Degr") label /// 
	drop(_cons school* MCIIp C2p WMTMCII C2MCII)

	
******* Table S2: Sample Balance: Regressing W1 Baseline Test Scores on the Treatment Indicator

est clear
eststo clear

** Run necessary regressions
qui foreach o in  Zw Obw Odw M4 M13 L R GfA Bpscore {
	
	reg w1`o'std WMTp MCIIp C2p WMTMCII C2MCII school2-school12 male agechild w1agetestday, vce(cluster classID)
	est store w1`o'1
	
}


** Generate table with regression results
esttab w1Zw1 w1Obw1 w1Odw1 w1M41 w1M131 w1L1 w1R1 w1GfA1 w1Bpscore1, ///
	nogaps b(3) se(3) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S2: Sample Balance: Regressing W1 Baseline Test Scores on the Treatment Indicator) ///
	mtitles("Verbal simple span" "Verbal complex span" "Visuo-spatial complex span" "Geometry" "Arithmetic" "Reading" "Raven's IQ" "Go/No-Go task" "bp task") ///
	collabels(none) label drop( _cons school* MCIIp C2p WMTMCII C2MCII male agechild w1agetestday)
	
	
	

****************** Run necessary regressions for Tables S3, S4, and S5 ******************

est clear
eststo clear

qui foreach o in Zw Obw Odw M13 M4 L R GfA Bpscore {
	
	foreach w in w2 w3 w4 {
		
		reg `w'`o'std WMTp MCIIp C2p WMTMCII C2MCII w1`o'std school2-school12 male agechild w1agetestday `w'agetestday, vce(cluster classID)
		est store `w'`o'1


	}
}




******* Table S3: Sample Balance: Treatment Effects on Working Memory Capacity

** Create table with regression results
esttab w2Zw1 w3Zw1 w4Zw1  w2Obw1 w3Obw1 w4Obw1 w2Odw1 w3Odw1 w4Odw1, /// 
	nogaps cells(b(star fmt(%9.3f)) se(par fmt(3)) p(par([ ]) fmt(3))) stats(N, fmt(0) labels("N")) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) title(Table S3: Sample Balance: Treatment Effects on Working Memory Capacity) /// 
	mtitles("W2" "W3" "W4" "W2" "W3" "W4" "W2" "W3" "W4") ///
	mgroups("Verbal simple span" "Verbal complex span" "Visuo-spatial complex span", ///
	pattern(1 0 0 1 0 0 1 0 0)) ///
	collabels(none) label ///
	drop(_cons school* w1*std male agechild w1agetestday w2agetestday w3agetestday w4agetestday MCIIp C2p WMTMCII C2MCII)

	
	
	
******* Table S4: Treatment Effects on Math, Reading, and Raven's IQ

esttab w2M131 w3M131 w4M131 w2M41 w3M41 w4M41 w2L1 w3L1 w4L1 w2R1 w3R1 w4R1, ///
	nogaps cells(b(star fmt(%9.3f)) se(par fmt(3)) p(par([ ]) fmt(3))) stats(N, fmt(0) labels("N")) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) title(Table S4: Treatment Effects on Math, Reading, and Raven's IQ) mtitles("W2" "W3" "W4" "W2" "W3" "W4" "W2" "W3" "W4" "W2" "W3" "W4") ///
	mgroups("Arithmetic" "Geometry" "Reading" "Raven's IQ", ///
	pattern(1 0 0 1 0 0 1 0 0 1 0 0)) collabels(none) label ///
	drop(_cons school* w1*std male agechild w1agetestday w2agetestday w3agetestday w4agetestday MCIIp C2p WMTMCII C2MCII)


	
	
******* Table S5: Treatment Effects on Performance in the Go/No-Go Task and bp Task

esttab w2GfA1 w3GfA1 w4GfA1 w2Bpscore1 w3Bpscore1 w4Bpscore1, ///
	nogaps cells(b(star fmt(%9.3f)) se(par fmt(3)) p(par([ ]) fmt(3))) stats(N, fmt(0) labels("N")) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) title(Table S5: Treatment Effects on Performance in the Go/No-Go Task and bp Task) 	mtitles("W2" "W3" "W4" "W2" "W3" "W4") mgroups("Go/No-Go Task" "bp task", ///
	pattern(1 0 0 1 0 0)) nonumbers collabels(none) label drop( _cons school* w1*std male agechild w1agetestday w2agetestday w3agetestday w4agetestday MCIIp C2p WMTMCII C2MCII)
	

	
	
****************** Run necessary regressions for Tables S6, S7, and S8 ******************

est clear
eststo clear

qui foreach o in Zw Obw Odw M13 M4 L R GfA Bpscore {
	
	foreach w in w2 w3 w4 {
		
		reg `w'`o'std WMTp MCIIp C2p WMTMCII C2MCII w1`o'std school2-school12 male agechild w1agetestday `w'agetestday w1WMw25 w1WMw25WMTp, vce(cluster classID)
		est store `w'`o'1
		
	}
}




******* Table S6: Heterogeneous Treatment Effects on Working Memory Capacity—Interaction with Low WM Capacity (W1)

esttab w2Zw1 w3Zw1 w4Zw1   w2Obw1 w3Obw1 w4Obw1   w2Odw1 w3Odw1 w4Odw1, ///
	nogaps b(3) se(3) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S6: Heterogeneous Treatment Effects on Working Memory Capacity—Interaction with Low WM Capacity (W1)) ///
	mtitles("W2" "W3" "W4" "W2" "W3" "W4" "W2" "W3" "W4" ) mgroups("Verbal simple span" "Verbal complex span" "Visuo-spatial complex span", pattern(1 0 0 1 0 0 1 0 0)) collabels(none) label ///
	drop(_cons school* w1*std male agechild w1agetestday w2agetestday w3agetestday w4agetestday MCIIp C2p WMTMCII C2MCII)

	
	
	
******* Table S7: Heterogeneous Treatment Effects on Math, Reading, and Raven's IQ—Interaction with Low WM Capacity (W1)

esttab w2M131 w3M131 w4M131   w2M41 w3M41 w4M41   w2L1 w3L1 w4L1   w2R1 w3R1 w4R1, ///
	nogaps b(3) se(3) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S7: Heterogeneous Treatment Effects on Math, Reading, and Raven's IQ—Interaction with Low WM Capacity (W1)) ///
	mtitles("W2" "W3" "W4" "W2" "W3" "W4" "W2" "W3" "W4" "W2" "W3" "W4") mgroups("Arithmetic" "Geometry" "Reading" "Raven's IQ", pattern(1 0 0 1 0 0 1 0 0 1 0 0)) collabels(none) label ///
	drop(_cons school* w1*std male agechild w1agetestday w2agetestday w3agetestday w4agetestday MCIIp C2p WMTMCII C2MCII)


	
	
******* Table S8: Heterogeneous Treatment Effects on Performance in the Go/No-Go Task and bp Task—Interaction with Low WM Capacity (W1)

esttab w2GfA1 w3GfA1 w4GfA1   w2Bpscore1 w3Bpscore1 w4Bpscore1, ///
	nogaps b(3) se(3) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S8: Heterogeneous Treatment Effects on Performance in the Go/No-Go Task and bp Task—Interaction with Low WM Capacity (W1)) ///
	mtitles("W2" "W3" "W4" "W2" "W3" "W4") mgroups("Go/No-Go task" "bp task", pattern(1 0 0 1 0 0)) /// 
	collabels(none) label ///
	drop(_cons school* w1*std male agechild w1agetestday w2agetestday w3agetestday w4agetestday MCIIp C2p WMTMCII C2MCII)

	
	
	
******* Table S9: Corrections for Multiple Hypothesis Testing 
*The MHT-BRL p-values are based on bootstrap estimation conducted with R, not part of this do-file, code can be obtained upon request.


******* Table S10: Corrections for Multiple Hypothesis Testing — Direct vs. Spillover Effects
*The MHT-BRL p-values are based on bootstrap estimation conducted with R, not part of this do-file, code can be obtained upon request.



****************** Run necessary regressions for Tables S11 and S12 ******************

est clear
eststo clear

qui foreach o in  Zw Odw M4 L R GfA {
	
	reg w3`o'std WMTp MCIIp C2p WMTMCII C2MCII w1`o'std school2-school12 male agechild w1agetestday w3agetestday w3computerFreq, vce(cluster classID)
	est store w3`o'reg1

}

qui foreach o in  Zw Odw M4 L R GfA {
	
	reg w4`o'std WMTp MCIIp C2p WMTMCII C2MCII w1`o'std school2-school12 male agechild w1agetestday w4agetestday w4computerFreq, vce(cluster classID)
	est store w4`o'reg1
	
}




******* Table S11: Treatment Effects on Working Memory Capacity—Controlling for Computer Use in Class

esttab w3Zwreg1 w4Zwreg1   w3Odwreg1 w4Odwreg1, nogaps ///
	b(3) se(3) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S11: Treatment Effects on Working Memory Capacity—Controlling for Computer Use in Class) ///
	mtitles("W3" "W4" "W3" "W4") mgroups("Verbal simple span" "Visuo-spatial complex span", pattern(1 0 1 0)) ///
	collabels(none) label ///
	drop(school* w1*std _cons male agechild w1agetestday w3agetestday w4agetestday MCIIp C2p WMTMCII C2MCII)
	



******* Table S12: Treatment Effects on Geometry, Reading, Raven's IQ, and Go/No-Go Task—Controlling for Computer Use in Class

esttab  w3M4reg1 w4M4reg1  w3Lreg1 w4Lreg1   w3Rreg1 w4Rreg1   w3GfAreg1 w4GfAreg1, nogaps ///
	b(3) se(3) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S12: Treatment Effects on Geometry, Reading, Raven's IQ, and Go/No-Go Task—Controlling for Computer Use in Class) ///
	mtitles("W3" "W4" "W3" "W4" "W3" "W4" "W3" "W4") mgroups("Geometry" "Reading" "Raven's IQ" "Go/No-Go task", pattern(1 0 1 0 1 0 1 0)) ///
	collabels(none) label ///
	drop(school* w1*std _cons male agechild w1agetestday w3agetestday w4agetestday MCIIp C2p WMTMCII C2MCII)

	
	

*************** Run necessary regressions for Tables S13, S14, and S15 ***************	

est clear
eststo clear

** Run necessary regressions
foreach o in  Zw Obw Odw M13 M4 L R GfA Bpscore {
	
	foreach w in w2 w3 w4 {
		
		qui reg `w'`o'std WMTp MCIIp C2p WMTMCII C2MCII w1`o'std school2-school12 male agechild w1agetestday `w'agetestday if w2`o'std<. & w3`o'std<. & w4`o'std<., vce(cluster classID)
		est store `w'`o'1
		
	}
}




******* Table S13: Treatment Effects on Working Memory Capacity—Sample Reduced to Children Appearing in All Waves

esttab w2Zw1 w3Zw1 w4Zw1   w2Obw1 w3Obw1 w4Obw1   w2Odw1 w3Odw1 w4Odw1, ///
	nogaps b(3) se(3) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S13: Treatment Effects on Working Memory Capacity—Sample Reduced to Children Appearing in All Waves) ///
	mtitles("W2" "W3" "W4" "W2" "W3" "W4" "W2" "W3" "W4") mgroups("Verbal simple span" "Verbal complex span" "Visuo-spatial complex span", pattern(1 0 0 1 0 0 1 0 0)) ///
	collabels(none) label ///
	drop(school* w1*std _cons male agechild w1agetestday w2agetestday w3agetestday w4agetestday MCIIp C2p WMTMCII C2MCII)
	

	
	
******* Table S14: Treatment Effects on Math, Reading, and Raven's IQ—Sample Reduced to Children Appearing in All Waves

esttab w2M131 w3M131 w4M131   w2M41 w3M41 w4M41   w2L1 w3L1 w4L1   w2R1 w3R1 w4R1, /// 
	nogaps b(3) se(3) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S14: Treatment Effects on Math, Reading, and Raven's IQ—Sample Reduced to Children Appearing in All Waves) ///
	mtitles("W2" "W3" "W4" "W2" "W3" "W4" "W2" "W3" "W4" "W2" "W3" "W4") mgroups("Arithmetic" "Geometry" "Reading" "Raven's IQ", pattern(1 0 0 1 0 0 1 0 0 1 0 0)) ///
	collabels(none) label ///
	drop(school* w1*std _cons male agechild w1agetestday w2agetestday w3agetestday w4agetestday MCIIp C2p WMTMCII C2MCII)


	
	
******* Table S15: Treatment Effects on Performance in the Go/No-Go Task and bp Task—Sample Reduced to Children Appearing in All Waves

esttab w2GfA1 w3GfA1 w4GfA1  w2Bpscore1 w3Bpscore1 w4Bpscore1, ///
	nogaps b(3) se(3) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S15: Treatment Effects on Performance in the Go/No-Go Task and bp Task—Sample Reduced to Children Appearing in All Waves) mtitles("W2" "W3" "W4" "W2" "W3" "W4") ///
	mgroups("Go/No-Go task" "bp task", pattern(1 0 0 1 0 0)) ///
	collabels(none) label ///
	drop(school* w1*std _cons male agechild w1agetestday w2agetestday w3agetestday w4agetestday MCIIp C2p WMTMCII C2MCII)



	
******* Table S16: Treatment Effect on Parental Investment W3

est clear
eststo clear

** Run necessary regression
qui reg W3_investment WMTp MCIIp C2p WMTMCII C2MCII school2-school12 male agechild, vce(cluster classID)
est store investment1

** Create table with regression results
esttab investment1, ///
	nogaps cells(b(star fmt(%9.3f)) se(par fmt(3))) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S16: Treatment Effect on Parental Investment W3) ///
	mtitle("Parental investment") collabels(none) nonumbers label ///
	drop(school* male agechild MCIIp C2p WMTMCII C2MCII _cons)


	
	
******* Table S17: Treatment Effects on Children's Self-Reported Motivation (W4)

est clear
eststo clear

** Run necessary regressions
qui foreach x in Q1 Q2 {
	
	reg w4_`x' WMTp MCIIp C2p WMTMCII C2MCII school2-school12 male agechild w1agetestday w4agetestday if w4_`x'>0, vce(cluster classID)
	est store reg`x'
	
}

** Create table with regression results
esttab regQ1 regQ2, ///
	nogaps b(3) se(3) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S17: Treatment Effects on Children's Self-Reported Motivation (W4)) ///
	mtitles("Enjoyment (W4)" "Effort (W4)") ///
	collabels(none) label ///
	drop(school* _cons male agechild w1agetestday w4agetestday MCIIp C2p WMTMCII C2MCII)


	
	
******* Table S18: Factor Analysis of Overall Self-regulation Items (W1)

** Perform Factor Analysis
qui factor w1enduringstd w1mistakesstd w1disciplinestd w1waitingstd w1disturbsstd w1homeworkstd w1patientstd

** Create Eigenvalue table
esttab, cells("Ev[Eigenvalues]") nogap noobs nonumber nomtitle title(Table S18: Factor Analysis of Overall Self-regulation Items (W1))

** Create factor values table
esttab, cells("L[Factor1](t label(Factor 1)) L[Factor2](t label(Factor 2))") nogap noobs nonumber nomtitle label title(Table S18: Factor Analysis of Overall Self-regulation Items (W1)) 
	

	
	
******* Table S19: Treatment Effects on Overall Self-regulation

est clear
eststo clear

**Run necessary regressions
qui foreach o in selfregulation {
	
	forval w=2/4 {
		
		reg w`w'`o'std WMTp MCIIp C2p WMTMCII C2MCII school2-school12 w1`o'std male agechild w1ageteacherq w`w'ageteacherq, vce(cluster classID)
		est store w`w'`o'std
		
	}
}
	

** Create table with regression results

esttab w2selfregulationstd w3selfregulationstd w4selfregulationstd, nogaps ///
	cells(b(star fmt(%9.3f)) se(par fmt(3))) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S19: Treatment Effects on Overall Self-regulation) ///
	mtitles("W2" "W3" "W4")  ///
	collabels(none) label drop( _cons school* w1selfregulationstd male agechild w1ageteacherq w2ageteacherq w3ageteacherq w4ageteacherq MCIIp C2p WMTMCII C2MCII)

	

	
******* Table S20: The Relevance of Working Memory Capacity in Geometry, Reading, Raven's IQ, and Go/No-Go Task

est clear
eststo clear

** Run necessary regressions

preserve

keep if WMTp == 0

qui foreach o in M4 L R GfA{
	
	reg w4`o'std w4Odwstd  w4Zwstd MCIIp C2p WMTMCII C2MCII w1`o'std school2-school12 male agechild w1agetestday w4agetestday, vce(cluster classID)
	est store w4`o'1
	
}

restore

** Create table with regression results
esttab w4M41 w4L1 w4R1 w4GfA1, ///
	nogaps b(3) cells(b(star fmt(%9.3f)) se(par fmt(3)) p(fmt(3) par([ ]))) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S20: The Relevance of Working Memory Capacity in Geometry, Reading, Raven's IQ, and Go/No-Go Task) ///
	collabels(none) nonumbers label ///
	drop(school* w1*std male agechild w1agetestday w4agetestday MCIIp C2p WMTMCII C2MCII _cons)	
	

	
	
******* Table S21: Treatment Effect on Probability of Being in Sample of Secondary School Choice-Interactions between Socio-Demographic Characteristics and Treatment Status

est clear
eststo clear

** Run necessary regressions
qui foreach o in male agechild mig pbsprache12 income45 Muni {
	
	reg gymEnonmiss WMTp `o' `o'WMTp MCIIp C2p WMTMCII C2MCII school2-school12, vce(cluster classID)
	est store `o'1

}

** Create table with regression results
esttab male1 agechild1 mig1 pbsprache121 income451 Muni1, nogaps ///
	cells(b(star fmt(%9.3f)) se(par fmt(3))) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S21: Treatment Effect on Probability of Being in Sample of Secondary School Choice-Interactions between Socio-Demographic Characteristics and Treatment Status) collabels(none) nonumber mtitle("(1)" "(2)" "(3)" "(4)" "(5)" "(6)") ///
	label drop( _cons school* MCIIp C2p WMTMCII C2MCII income45 Muni mig pbsprache12 male agechild) 

	
	
	
******* Table S22: Treatment Effect on Probability of Being in Sample of Secondary School Choice-Interactions between Outcome Scores at Baseline and Treatment Status

est clear
eststo clear

** Run necessary regressions 
qui foreach o in Zw Obw Odw M13 M4 L R GfA Bpscore {
	
	reg gymEnonmiss WMTp w1`o'std w1`o'stdWMTp MCIIp C2p WMTMCII C2MCII school2-school12 male agechild w1agetestday, vce(cluster classID)
	est store `o'1

}

** Create table with regression results
esttab Zw1 Obw1 Odw1 M131 M41 L1 R1 GfA1 Bpscore1, nogaps ///
	cells(b(star fmt(%9.3f)) se(par fmt(3))) stats(N, fmt(0) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Table S22: Treatment Effect on Probability of Being in Sample of Secondary School Choice-Interactions between Outcome Scores at Baseline and Treatment Status) ///
	mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)" "(9)") collabels(none) nonumbers label drop( _cons school* w1agetestday MCIIp C2p WMTMCII C2MCII male agechild w1Zwstd w1Obwstd w1Odwstd w1M13std w1M4std w1Lstd w1Rstd w1GfAstd w1Bpscorestd) 


	
	
******* Table S23: Sample Composition at W1, W4, and at the Time of Secondary School Track Choice

est clear
eststo clear

qui foreach o in w1_participant w4_participant gymEnonmiss {
	
	eststo column_`o': estpost tabstat WMTp male agechild w1agetestday w2agetestday w3agetestday w4agetestday mig pbsprache12 income1 income2 income3 income4 income5 Muni Mausb Mkausb w1Zwstd w1Obwstd w1Odwstd w1M13std w1M4std w1Lstd w1Rstd w1GfAstd  w1Bpscorestd if `o' == 1, stats(mean ) columns(statistics) 
		
}

** Create summary statistics table
esttab column_w1_participant column_w4_participant column_gymEnonmiss, cells("mean(fmt(3))") nonumbers title(Table S23: Sample Composition at W1, W4, and at the Time of Secondary School Track Choice) mtitle("Mean for Sample at W1" "Mean for Sample at W4" "Mean for Sample at School Track Choice") collabels(none) label noobs 



